<?php

namespace app\apisell\controller\admin;

use app\apisell\model\popuLink\PopuLinkModel;
use app\apisell\service\PopuLink\PopuLinkService;
use app\BaseController;
use app\common\utils\Res;
use think\facade\Db;

class PopuLink extends BaseController
{
    /**
     * 推广链接列表
     */
    public function getList()
    {
        $param = $this->request->param();
        $param['uid'] =  $this->request->uid;
        $is_popu = $this->request->isPopu;
        $result = PopuLinkService::getList($param,false,$is_popu);
        return Res::ok($result);
    }

    /**
     * 继承推广链接列表
     */
    public function getInheritList()
    {
        $param = $this->request->param();
        $param['uid'] =  $this->request->uid;
        $is_popu = $this->request->isPopu;
        $result = PopuLinkService::getList($param,true,$is_popu);
        return Res::ok($result);
    }

    /**
     * 创建推广链接
     */
    public function createPopuLink()
    {
        $param = $this->request->param();
        $param['add_id'] =  $this->request->uid; //登录的后台账户ID，不是推广ID
        $param['popu_id'] = $this->request->popuId; //推广员ID
        
        if($this->request->popuLevel != 0){
            $check = $this->checkPopuUser($param,'created');
            if($check['code'] == 0){
                return Res::error($check['msg']);
            }
        }
        
        //判断重复
        $have = PopuLinkModel::have($param['goods_id'],$param['popu_id']);
        if($have){
            return Res::error('不能添加重复的推广链接！');
        }
        
        //佣金校验
        $goods = Db::table('xh_sell_goods')->where('id',$param['goods_id'])->find();
        if(($param['link_divide_price'] * 100) > $goods['commission']){
            return Res::error('佣金不能大于商品设置的总佣金，请重新设置！');
        }
        
        
        $result = PopuLinkService::createPopuLink($param,$this->request->isPopu);
        return Res::ok($result);
    }
    
    /**
     * 查看  生成链接和二维码
     */
    public function checkPopuLink()
    {
        $param = $this->request->param();
        $param['popu_user_id'] =  $this->request->uid;
        $result = PopuLinkService::checkPopuLink($param);
        
        $result['is_popu'] = $this->request->isPopu;
        
        return Res::ok($result);
    }

    /**
     * 查询当前推广链接
     */
    public function viewPopuLink()
    {
        $param = $this->request->param();
        $result = PopuLinkService::viewPopuLink($param);
        return Res::ok($result);
    }

    /**
     * 编辑推广链接
     */
    public function editePopuLink()
    {
        $param = $this->request->param();
        $param['add_id'] =  $this->request->uid; //登录的后台账户ID，不是推广ID
        $popuLink = PopuLinkModel::viewPopuLink($param);
        
        if($this->request->isPopu > 1 && $this->request->popuLevel != 0){
            $check = $this->checkPopuUser($param,'edite');
            if($check['code'] == 0){
                return Res::error($check['msg']);
            }
        }
        
        //库存数量不得低于已售数量
        if($popuLink['sold_quantity'] > $param['goods_quantity']){
            return Res::error('库存数量设置错误！');
        }

        //佣金校验
        $goods = Db::table('xh_sell_goods')->where('id',$param['goods_id'])->find();
        if(($param['link_divide_price'] * 100) > $goods['commission']){
            return Res::error('佣金不能大于商品设置的总佣金，请重新设置！');
        }
        
        $result = PopuLinkService::editePopuLink($param);
        return Res::ok($result);
    }

    /**
     * 补货
     */
    public function addGoodsQuantity()
    {
        $param = $this->request->param();
        $param['uid'] =  $this->request->uid;
        if($this->request->isPopu > 1 && $this->request->popuLevel != 0){
            $check = $this->checkPopuUser($param,'buhuo');
            if($check['code'] == 0){
                return Res::error($check['msg']);
            }
        }
        $popuLink = PopuLinkModel::viewPopuLink($param);
        $result = PopuLinkService::addGoodsQuantity($param,$popuLink);
        return Res::ok($result);
    }

    /**
     * 修改状态
     */
    public function editeLinkStatus()
    {
        $param = $this->request->param();
        $param['uid'] =  $this->request->uid;
        if($this->request->isPopu > 1 && $this->request->popuLevel != 0){
            $check = $this->checkPopuUser($param,'change_type');
            if($check['code'] == 0){
                return Res::error($check['msg']);
            }
        }
        $result = PopuLinkService::editeLinkStatus($param);
        return Res::ok($result);
    }

    /**
     * 获取商品
     */
    public function getGoodsList()
    {
        $param = $this->request->param();
        $result = PopuLinkService::getGoodsList($param);
        return Res::ok($result);
    }
    
    //判断分销用户权限
    private function checkPopuUser($param,$type)
    {
        $result = [
            'code' => 1,
            'msg' => ''
        ];
        if($type != 'created'){
            $popuLink = PopuLinkModel::viewPopuLink($param);
        }
        
        switch($type){
            case 'created':
                if($this->request->isPopu > 1 && $this->request->popuLevel == 2){
                    $result['code'] = 0;
                    $result['msg'] = '你没有权限创建，请联系平台！';
                }
                break;
            case 'edite':
                if($popuLink['link_level'] == 1){
                    $result['code'] = 0;
                    $result['msg'] = '你没有权限编辑，请联系平台！';
                }else{
                    if($popuLink['add_id'] != $this->request->uid){
                        $result['code'] = 0;
                        $result['msg'] = '你没有权限编辑，请联系平台！';
                    }
                }
                break;
            case 'buhuo':
                if($popuLink['link_level'] == 1){
                    $result['code'] = 0;
                    $result['msg'] = '你没有权限补货，请联系平台！';
                }else{
                    if($popuLink['add_id'] != $this->request->uid){
                        $result['code'] = 0;
                        $result['msg'] = '你没有权限补货，请联系平台！';
                    }
                }
                break;
            case 'change_type':
                if($popuLink['link_level'] == 1){
                    $result['code'] = 0;
                    $result['msg'] = '你没有权限更改状态，请联系平台！';
                }else{
                    if($popuLink['add_id'] != $this->request->uid){
                        $result['code'] = 0;
                        $result['msg'] = '你没有权限更改状态，请联系平台！';
                    }
                }
                break;
        }
        return $result;
    }
    
    
}